//
//  IMB_NetWorkJsonRespOperation.m
//  InvestChina
//
//  Created by 闫建刚 on 14-3-30.
//  Copyright (c) 2014年 iMobile. All rights reserved.
//

#import "IMB_NetWorkJsonRespOperation.h"

@implementation IMB_NetWorkJsonRespOperation

/**
 *  网络访问完毕
 */
- (void)netAccessComplete{
    dispatch_sync(dispatch_get_main_queue(), ^{
        
        // add by jason yan 2014-05-29
        [self willChangeValueForKey:@"isFinished"];
        [self willChangeValueForKey:@"isExecuting"];
        
        // commit by jason yan 2014-05-29
        /*NSError *err;
        if(message_){
            block_(nil,message_);
        }else if(responseData_.length > 0){
            
            NSString *responseDataStr = [[NSString alloc]initWithData:responseData_ encoding:NSUTF8StringEncoding];
            NSLog(@"responseDataStr:%@",responseDataStr);
            
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseData_ options:NSJSONReadingMutableContainers error:&err];
            if (err) {
#warning 测试阶段显示系统错误，记得发布前替换
              block_(nil,NET_ERROR_MSG_FOR_RESPONSE_JSON_DATA);
            }else{
                block_(dict,nil);
            }
        }*/
        
        NSError *err;
        if(responseData_.length > 0){
            //#warning 测试阶段显示
            NSString *responseDataStr = [[NSString alloc]initWithData:responseData_ encoding:NSUTF8StringEncoding];
            NSLog(@"responseDataStr:%@",responseDataStr);
            
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseData_ options:NSJSONReadingMutableContainers error:&err];
            if (err) {
                
                block_(nil,NET_ERROR_MSG_FOR_RESPONSE_JSON_DATA);
            }else{
                // update by jason yan 2014-04-29
                block_(dict,message_);
            }
        }else{
            block_(nil,message_);
        }
        
        // add by jason yan 2014-05-29
        executing_ = NO;
        finished_ = YES;
        [self didChangeValueForKey:@"isExecuting"];
        [self didChangeValueForKey:@"isFinished"];
        
    });
}


- (BOOL)canCacheData:(NSCachedURLResponse*)cachedResponse{
    NSError *err;
    [NSJSONSerialization JSONObjectWithData:[cachedResponse data] options:NSJSONReadingMutableContainers error:&err];
    if (err) {
        return NO;
    }
    return YES;
}


@end
